<template>
  <div v-if="isSuper === 0" style="text-align: center; margin-top: 50px;">
    您的权限不够,请联系管理员升级权限。hzshenpeng@corp.netease.com
  </div>
  <div v-else>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <!-- <el-input v-model="searchModel.username" placeholder="用户名" clearable />
          <el-input v-model="searchModel.phone" placeholder="电话" clearable /> -->
          <el-button type="primary" @click="getFpdList" round icon="el-icon-search">查询</el-button>
        </el-col>
        <el-col v-if="isSuper === 1" :span="4" align="right">
          <el-button @click="editFpd(null)" type="primary" icon="el-icon-plus" circle></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 查询结果栏 -->
    <el-card>
      <el-table :data="Fpd_list" border style="width: 100%">
        <el-table-column label="#" width="100" align="center">
          <template slot-scope="scope">
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="course_name" label="科目名称" align="center">
        </el-table-column>
        <el-table-column prop="fly_exam_name" label="飞检试卷名" align="center">
        </el-table-column>
        <el-table-column prop="question_type" label="试题类型" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.question_type == `xz`" type="success">单选</el-tag>
            <el-tag v-else-if="scope.row.question_type == `mxz`" type="warning">多选</el-tag>
            <el-tag v-else-if="scope.row.question_type == `pd`" type="info">判断</el-tag>
            <el-tag v-else-if="scope.row.question_type == `tk`" type="primary">填空</el-tag>
            <el-tag v-else-if="scope.row.question_type == `jd`" type="danger">简答</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="question_id" label="试题ID" align="center">
        </el-table-column>
        <el-table-column prop="content" label="试题题目" width="500" align="center">
        </el-table-column>
        <el-table-column v-if="isSuper === 1" label="编辑" width="200" align="center">
          <template slot-scope="scope">
            <el-button @click="editFpd(scope.row.id)" type="primary" icon="el-icon-edit" circle size="mini"></el-button>
            <el-button @click="open(scope.row)" type="danger" icon="el-icon-delete" circle size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo" :page-sizes="[5, 10, 50, 100]" :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 弹出框 -->
    <el-dialog @close="clearForm" :title="title" :visible.sync="dialogFormVisible">
      <!-- <el-dialog :title="title" :visible.sync="dialogFormVisible"> -->
      <el-form :model="FpdForm">
        <!-- <el-form-item label="科目名称：" prop="Fpd_name" v-if="FpdForm.id == null || FpdForm.id == undefined"
          :label-width="formLabelWidth">
          <el-input v-model="FpdForm.Fpd_name" autocomplete="off"></el-input>
        </el-form-item> -->
        <el-form-item label="飞检试卷名:" prop="fly_exam_name" :label-width="formLabelWidth">
          <el-select v-model="FpdForm.paper_name_id" filterable placeholder="飞检试卷名" clearable>
            <el-option v-for=" item  in  fly_exam_name_list " :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="科目名称:" prop="course_name" :label-width="formLabelWidth">
          <el-select v-model="FpdForm.course_id" filterable placeholder="科目名称" clearable @change="handleSelectChange()"
            @clear="handleClear">
            <el-option v-for=" item  in  course_list " :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="试题类型:" prop="question_type" :label-width="formLabelWidth">
          <el-select v-model="FpdForm.question_type" :key="FpdForm.question_type" filterable placeholder="试题类型" clearable
            @change="handleSelectChange()" @clear="handleClear">
            <el-option v-for=" item  in  question_type_list " :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="试题ID:" prop="question_id" :label-width="formLabelWidth">
          <el-select v-model="FpdForm.question_id" filterable placeholder="试题ID"
            @change="handleSelectChangeById(FpdForm.question_id)">
            <el-option v-for=" item  in question_id_list " :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="试题题目：" prop="content" :label-width="formLabelWidth">
          <!-- <el-input v-model="fpdForm.content" autocomplete="off"></el-input> -->
          <textarea v-model="FpdForm.content" autocomplete="off" rows="3" class="custom-width" readonly></textarea>
        </el-form-item>
        <!-- <el-form-item label="科目名称:" prop="course_name" :label-width="formLabelWidth">
          <el-input v-model="FpdForm.name" autocomplete="off"></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="飞检试卷名:" prop="fly_exam_name"  :label-width="formLabelWidth">
          <el-input v-model="FpdForm.desc" autocomplete="off"></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="试题ID:" prop="question_id"  :label-width="formLabelWidth">
          <el-input v-model="FpdForm.question_id" autocomplete="off"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="addFpd" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import testApi from "@/api/testMange";
import { mapGetters } from 'vuex';
export default {
  data() {
    return {
      FpdForm: {
        // "idc_name": "",
        // "is_active": 0,
      },
      formLabelWidth: "130px",
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      Fpd_list: [],
      course_list: [],
      fly_exam_name_list: [],
      question_id_list: [],
      question_type_list: [
        {
          value: 'xz',
          label: '单选'
        }, {
          value: 'mxz',
          label: '多选'
        }, {
          value: 'tk',
          label: '填空'
        }, {
          value: 'jd',
          label: '简答'
        }, {
          value: 'pd',
          label: '判断'
        }
      ]
    };
  },
  methods: {
    handleSelectChange() {
      // This method will be called when the value in el-select changes
      // You can perform your desired actions here
      // console.log('Selected value:', this.fpdForm.course_id);
      // Call your click method or perform other actions as needed
      this.getQuestionId();
      if (this.FpdForm.question_id != undefined) {
        this.FpdForm.question_id = "";
      }
      this.FpdForm.content = "";
    },
    getQuestionId() {
      // Your click method logic goes here
      // console.log('Click method triggered!');
      testApi.getQuestionList(this.FpdForm).then((response) => {
        this.question_id_list = response.data.api_question_id_list;
      });
    },
    handleClear() {
      this.FpdForm.content = "";
    },
    async handleSelectChangeById(id) {
      await this.getQuestionById(id);
      // this.fpdForm.course_id = this.course_id;
      // this.fpdForm.question_type = this.question_type;
      this.$forceUpdate();
    },
    async getQuestionById(id) {
      try {
        const response = await testApi.getQuestionById(id);
        this.FpdForm.course_id = response.data.course_id;
        this.FpdForm.question_type = response.data.questionType;
        this.FpdForm.content = response.data.content;
      } catch (error) {
        console.error('Error fetching question by ID:', error);
      }
    },
    open(Fpd) {
      this.$confirm(`您是否确认删除该试题?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          testApi.deleteFpdById(Fpd.id).then((response) => {
            this.$message({
              type: "success",
              message: response.message,
            });
            this.getFpdList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addFpd() {
      testApi.saveFpd(this.FpdForm).then((reponse) => {
        // 关闭弹窗
        this.dialogFormVisible = false;
        // 提示提交成功
        this.$message({
          message: reponse.message,
          type: "success",
        });
        // 刷新页面
        this.getFpdList();
      });
    },
    clearForm() {
      this.FpdForm = {
        // "idc_name": "",
        // "is_active": 0,
      };
      this.getFpdList();
    },
    editFpd(id) {
      if (id == null) {
        this.title = "新增试题";
      } else {
        this.title = "修改试题";
        //先从后端获取user信息
        testApi.getFpdById(id).then((response) => {
          this.FpdForm = response.data;
        });
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getFpdList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getFpdList();
    },
    getFpdList() {
      testApi.getFpdList(this.searchModel).then((response) => {
        this.total = response.data.total;
        this.Fpd_list = response.data.api_fpd_list;
        this.course_list = response.data.api_course_list;
        this.fly_exam_name_list = response.data.api_fly_paper_list;
        this.question_id_list = response.data.api_question_id_list;
      });
    },
  },
  created() {
    this.getFpdList();
  },
  computed: {
    ...mapGetters([
      // 'name',
      'isSuper',
      // 'nick_name'
    ]),
  }
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 88%;
}

.custom-width {
  width: 88%;
  /* 或者其他你想要的宽度值 */
}
</style>